"""
@Author: li
@Email: lijianqiao2906@live.com
@FileName: user.py
@DateTime: 2025/07/06
@Docs: 用户相关的 Pydantic Schema
"""

from datetime import datetime

from pydantic import EmailStr, Field

from app.schemas.base import BaseRequest
from app.schemas.role import RoleRead


class UserBase(BaseRequest):
    """用户基础 Schema"""

    username: str = Field(max_length=50, description="用户名")
    email: EmailStr = Field(max_length=100, description="邮箱")
    phone: str | None = Field(default=None, max_length=20, description="手机号")
    nickname: str | None = Field(default=None, max_length=100, description="昵称")
    avatar_url: str | None = Field(default=None, max_length=500, description="头像URL")
    bio: str | None = Field(default=None, description="个人简介")
    description: str | None = Field(default=None, description="描述")


class UserCreate(UserBase):
    """创建用户 Schema"""

    password: str = Field(min_length=6, max_length=50, description="密码")
    role_ids: list[int] = Field(default_factory=list, description="关联的角色ID列表")


class UserUpdate(BaseRequest):
    """更新用户 Schema (所有字段可选)"""

    username: str | None = Field(default=None, max_length=50, description="用户名")
    email: EmailStr | None = Field(default=None, max_length=100, description="邮箱")
    phone: str | None = Field(default=None, max_length=20, description="手机号")
    nickname: str | None = Field(default=None, max_length=100, description="昵称")
    avatar_url: str | None = Field(default=None, max_length=500, description="头像URL")
    bio: str | None = Field(default=None, description="个人简介")
    is_active: bool | None = Field(default=None, description="是否激活")
    is_superuser: bool | None = Field(default=None, description="是否超级用户")
    role_ids: list[int] | None = Field(default=None, description="关联的角色ID列表")


class UserUpdatePassword(BaseRequest):
    """更新用户密码 Schema"""

    old_password: str = Field(description="旧密码")
    new_password: str = Field(min_length=6, max_length=50, description="新密码")


class AssignRolesRequest(BaseRequest):
    """分配角色请求 Schema"""

    role_ids: list[int] = Field(description="要分配的角色ID列表")


class UserRead(UserBase):
    """读取用户 Schema"""

    id: int = Field(description="用户ID")
    is_active: bool = Field(description="是否激活")
    is_superuser: bool = Field(description="是否超级用户")
    last_login_at: datetime | None = Field(default=None, description="最后登录时间")
    roles: list[RoleRead] = Field(default_factory=list, description="关联的角色列表")

    class Config:
        from_attributes = True
